<template>
  <div>
    <a-card :bordered="false">
      <f-search-form :more="true" @searchHandler="searchTable">
        <f-search-form-item label="部门" name="deptId" type="select" :data="api.dept.list" valueKey="deptId"
                            label-key="deptName"/>
        <f-search-form-item label="姓名" name="saleManName" slot="advanced"/>
        <f-search-form-item label="电话" name="linkTel" slot="advanced"/>
      </f-search-form>
      <f-table ref="table" rowKey="saleManId" :columns="columns" :multiple="true" :operator="operator"
               :params="queryParam" :api="api.saleman" handler='busSaleManPageListHandler'>
          <span slot="deptManager" slot-scope="text, record, index">
            <a-tag color="#108ee9" v-if="text==0">否</a-tag>
            <a-tag color="#f50" v-if="text==1">是</a-tag>
        </span>
        <span slot="salemanState" slot-scope="text, record, index">
          <a-switch v-model="record.salemanState==1" checkedChildren="启用" unCheckedChildren="禁用"
                    @change="(checked) => setSalemanState(checked,record)"/>
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="$refs.drawer.showDrawer(record)">编辑</a>
            <a-divider type="vertical"/>
          </template>
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down"/></a>
            <a-menu slot="overlay">
              <a-menu-item v-if="$auth('table.delete')">
                <a href="javascript:;" @click="$refs.table.del(record)">删除</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </f-table>
      <saleman-drawer ref="drawer"></saleman-drawer>
    </a-card>
  </div>
</template>
<script>
  import salemanDrawer from './saleman_drawer.vue'
  import api from './js/api'

  export default {
    name: 'TableList',
    components: {
      salemanDrawer
    },
    data() {
      let me = this
      //操作按钮
      let operator = []
      operator.push({
        name: '添加',
        icon: 'plus',
        that: this,
        handler: function(table, that) {
          const params = {'deptId':null }
          if (that.searchParams&&that.searchParams.deptId) {
            params.deptId = that.searchParams.deptId
          }
          that.$refs.drawer.showDrawer(params)
        }
      })
      operator.push({
        name: '删除',
        icon: 'delete',
        batch: true,
        handler: function(table) {
          table.remove()
        }
      })
      return {
        api: Object.assign(api, this.api),
        //列表初始化查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '#',
            width: '60',
            align: 'center',
            customRender: (text, record, index) => index + 1
          },
          {
            title: '业务员姓名',
            align: 'center',
            dataIndex: 'saleManName'
          },
          {
            title: '机构',
            align: 'center',
            dataIndex: 'organizationName'
          },
          {
            title: '账套',
            align: 'center',
            dataIndex: 'companyName'
          },
          {
            title: '部门',
            align: 'center',
            dataIndex: 'deptName'
          },
          {
            title: '联系电话',
            align: 'center',
            dataIndex: 'linkTel'
          },
          {
            title: '状态',
            align: 'center',
            scopedSlots: { customRender: 'salemanState' },
            dataIndex: 'salemanState'
          },
          {
            title: '操作',
            align: 'center',
            scopedSlots: { customRender: 'action' }
          }
        ],
        operator: operator
      }
    },
    created() {
    },
    methods: {
      //设置状态
      setSalemanState(check, record) {
        this.http.post(this, {
          url: this.api.saleman.setSalemanState,
          data: {
            ids: record.saleManId,
            salemanState: Number(check)
          },
          success(me, data) {
            record.salemanState = data
          }
        })
      },
      searchTable(searchParams) {
        this.searchParams = searchParams
        this.$refs.table.refresh(true, searchParams)
      }
    }
  }
</script>
